<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="类型" prop="isXt">
        <el-select placeholder="请选择类型" v-model="queryParams.isXt" @keyup.enter.native="handleQuery">
          <el-option label="销售单" value="3" />
          <el-option label="销退单" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="saleList">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="单号" align="center" prop="saleCode" />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column label="总数量" align="center" prop="znum" />
      <el-table-column label="总金额" align="center" prop="zprice" />
      
   <el-table-column label="类型" align="center" prop="isXt">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isXt === '3'">销售单</el-tag>
          <el-tag  v-if="scope.row.isXt === '4'">销退单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="是否入库" align="center" prop="isRk">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isRk === '0'">未入库</el-tag>
          <el-tag type="success" v-if="scope.row.isRk === '1'">入库</el-tag>
          <el-tag type="danger" v-if="scope.row.isRk === '2'">未出库</el-tag>
          <el-tag type="success" v-if="scope.row.isRk === '3'">出库</el-tag>
          <el-tag type="warning" v-if="scope.row.isRk === '4'">未全入库</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.isXt !== '3'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="rk(scope.row)"
            v-hasPermi="['supermarket:sale:inStore']"
            >入库</el-button
          >

          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="outStore(scope.row)"
            v-hasPermi="['supermarket:sale:outStore']"
            >出库</el-button
          >

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer title="采购详情" :visible.sync="drawer" size="80%">
      <el-table
        v-loading="loading"
        :data="xqsaleList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号id" align="center" prop="xqsaleId" />
        <el-table-column label="汇总id" align="center" prop="saleId" />
        <el-table-column label="商品id" align="center" prop="productId" />
        <el-table-column label="数量" align="center" prop="sum" />
        <el-table-column label="赠品数量" align="center" prop="zpSum" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="总金额" align="center" prop="zprice" />
        <el-table-column label="是否入库" align="center" prop="isRk" />
        <!-- <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">

            <el-popover ref="popoverRef" placement="right" width="400" trigger="click" v-if="scope.row.isRk === '0'">
              <el-form ref="form" :model="form" :rules="rules">
                <el-form-item label="货位">
                  <treeselect
                  v-model="form.whId"
                :options="warehouseOptions"
                :normalizer="normalizer"
                placeholder="请选择货位"
              />
                </el-form-item>
                <el-button type="primary" @click="rk(scope.row)">确 定</el-button>
              </el-form>
              <el-button slot="reference">click 激活</el-button>
            </el-popover>

            <span v-else>已入库</span>
          </template>
        </el-table-column> -->
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-drawer>

    <!-- 添加或修改采购采退汇总对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单号" prop="purchaseCode">
          <el-input v-model="form.purchaseCode" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="供应商id" prop="supplyId">
          <el-input v-model="form.supplyId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="总数量" prop="znum">
          <el-input v-model="form.znum" placeholder="请输入总数量" />
        </el-form-item>
        <el-form-item label="总金额" prop="zprice">
          <el-input v-model="form.zprice" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="采购or采退" prop="isCt">
          <el-input v-model="form.isCt" placeholder="请输入采购or采退" />
        </el-form-item>
        <el-form-item label="是否支付" prop="isZf">
          <el-input v-model="form.isZf" placeholder="请输入是否支付" />
        </el-form-item>
        <el-form-item label="是否入库" prop="isRk">
          <el-input v-model="form.isRk" placeholder="请输入是否入库" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listSale,saleOutStore,saleInStore ,getSale, delSale, addSale, updateSale } from "@/api/supermarket/sale";
import { listXqsale, getXqsale, delXqsale, addXqsale, updateXqsale } from "@/api/supermarket/xqsale";
import { listWarehouse } from "@/api/supermarket/warehouse";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Purchase",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      drawer: false,
       saleList: [],
      xqsaleList: [],
      //货位列表
      warehouseOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        isCt: null,
        isZf: null,
        isRk: null,
        isSh: 0 
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {

    gethwList() {
      listWarehouse(this.queryParams).then(response => {
         this.warehouseOptions =  this.handleTree(response.rows, "whId");
      });
    },
     /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.whId,
        label: node.whName,
        children: node.children,
      };
    },
    /** 查询采购采退汇总列表 */
    getList() {
      this.loading = true;
      listSale(this.queryParams).then((response) => {
        this.saleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // getListXq(saleId) {
    //   this.queryParams.saleId = saleId
    //   listXqsale(this.queryParams).then((response) => {
    //     this.xqsaleList = response.rows;
    //     this.total = response.total;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        saleId: null,
        purchaseCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        createTime: null,
        isCt: null,
        isZf: null,
        isRk: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.purchaseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购采退汇总";
    },
    /** 修改按钮操作 */

        // rk(row) {
    //   this.form.pId = row.pId;
    //   this.form.purchaseId = row.purchaseId
    //   ark(this.form).then((res) => {
    //     this.$modal.msgSuccess("入库成功");
    //     this.getListXq();
    //   })
    // },
    rk(row) {
      saleInStore(row.saleId).then(res => {
        this.$modal.msgSuccess("全部入库成功");
        this.getList();
      })
      // this.drawer = true;
      // this.getListXq(row.saleId);
      // this.gethwList();
      // this.reset();
      // const purchaseId = row.purchaseId || this.ids
      // getPurchase(purchaseId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改采购采退汇总";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate((valid) => {
      //   if (valid) {
      //     if (this.form.purchaseId != null) {
      //       updatePurchase(this.form).then((response) => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addPurchase(this.form).then((response) => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const purchaseIds = row.purchaseId || this.ids;
      // this.$modal
      //   .confirm(
      //     '是否确认删除采购采退汇总编号为"' + purchaseIds + '"的数据项？'
      //   )
      //   .then(function () {
      //     return delPurchase(purchaseIds);
      //   })
      //   .then(() => {
      //     this.getList();
      //     this.$modal.msgSuccess("删除成功");
      //   })
      //   .catch(() => {});
    },
    storage(row) { },
    //出库操作
    outStore(row) {
      saleOutStore(row.saleId).then(res => {
        this.$modal.msgSuccess("出库成功");
        this.getList();
      })
      // purchaseOutStore(row.purchaseId).then(res => {
      //   console.log(res);
      // })
    }
  },
};
</script>
